package com.wdcloud.netlibrary.request;

import java.util.Map;

/**
 * get请求
 * <p>
 */

public class GetRequest extends BaseRequest<GetRequest> {

    @Override
    public void buildRequest() {
        if (url == null) {
            throw new RuntimeException("请求路径不能为空");
        }
        StringBuilder sb = new StringBuilder();
        if (params != null) {
            if (url.contains("?") && !url.endsWith("?") && !url.endsWith("&")) {
                sb.append("&");
            } else if (!url.contains("?")) {
                sb.append("?");
            }

            for (Map.Entry<String, String> entry : params.entrySet()) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }

        String sbStr = sb.toString();
        if (sbStr.endsWith("?") || sbStr.endsWith("&")) {
            sb.deleteCharAt(sb.length() - 1);
        }

        builder.url(url + sb.toString());
    }
}
